package com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.group;

import com.alibaba.fastjson.TypeReference;
import com.tbit.common.utils.JsonUtils;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.ConditionLogic;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.PerformanceBizType;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.PerformanceRuleType;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PerformanceRule;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PerformanceRuleGroup;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.BizRuleGroup;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.rule.IntervalAndValidityBasedRule;

import lombok.Data;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @description: 订单时间段/时效的配置
 * @author: qiu.longjie
 * @create: 2025-04-07
 */
@Data
public class IntervalAndValidityBasedGroup implements BizRuleGroup {
    /**
     * 是否启用
     */
    private Boolean enabled;

    /**
     * 最多奖励订单数
     */
    private Integer maxOrderCount;

    /**
     * 绩效规则
     */
    private List<IntervalAndValidityBasedRule> rules;

    @Override
    public PerformanceRuleGroup toRaw(PerformanceBizType bizType, Integer accountId) {
        PerformanceRuleGroup group = new PerformanceRuleGroup();
        group.setAccountId(accountId);
        group.setBizType(bizType);
        group.setRuleType(PerformanceRuleType.INTERVAL_AND_VALIDITY);
        group.setEnabled(enabled);
        group.setRules(new ArrayList<>());

        if (rules == null) {
            return group;
        }
        PerformanceRule performanceRule = new PerformanceRule();
        performanceRule.setConditionLogic(ConditionLogic.AND);
        performanceRule.setConditions(new ArrayList<>());
        // 把rules转为json串，并存入metaInfo。所有规则都存在mateInfo，就一条rule记录
        Map<String, Object> ruleMetaInfo = new HashMap<>();
        ruleMetaInfo.put("rules", rules);
        performanceRule.setMetaInfo(ruleMetaInfo);
        performanceRule.setPerformance(BigDecimal.ZERO);
        group.getRules().add(performanceRule);

        if (maxOrderCount == null) {
            throw new BaseException("产生订单绩效-订单奖励上限单数不能为空");
        }
        Map<String, Object> metaInfo = new HashMap<>();
        metaInfo.put("maxOrderCount", maxOrderCount);
        group.setMetaInfo(metaInfo);

        return group;
    }

    @Override
    public void initFromRaw(PerformanceRuleGroup group) {
        setEnabled(group.getEnabled());
        setRules(new ArrayList<>());
        if (!ObjectUtils.isEmpty(group.getRules())) {
            // 该规则存在一个rule,规则内容存在metaInfo字段中
            PerformanceRule performanceRule = group.getRules().get(0);
            Map<String, Object> metaInfo = performanceRule.getMetaInfo();
            if (metaInfo != null) {
                Object rulesObj = metaInfo.get("rules");
                if (rulesObj != null) {
                    rules = JsonUtils.json2Obj(JsonUtils.obj2Json(rulesObj), new TypeReference<List<IntervalAndValidityBasedRule>>() {
                    });
                }
            }
        }

        if (group.getMetaInfo() != null) {
            setMaxOrderCount((Integer) group.getMetaInfo().get("maxOrderCount"));
        }
    }

    @Override
    public void initDefault() {
        enabled = false;
        maxOrderCount = 0;
        rules = Collections.emptyList();
    }
}
